/*********************************************************
          File Name:move.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Thu 17 Sep 2015 09:04:52 PM CST
**********************************************************/

#include <iostream>

class T
{
        private:
                int *n;
        public:
                // src.n = nullptr is necessary   
                // cause after std::move(), src.n will be a dangling pointer
                // delete a dangling pointer may cuase `corruption`
                // so, set it to nullptr and delete nullptr is no harm
                T(T &&src) noexcept : n(src.n) {src.n=nullptr;} 
                T(): n(new int(1)) {}
                T &operator=(T &&) noexcept;// do NOT throw exception
                void get()
                {
                        std::cout << *n << std::endl;
                }

                ~T()
                {
                        std::cout << "delete " << n << std::endl;
                        delete n;
                }
};

T &T::operator=(T &&src) noexcept
{
        if(this != &src)
        {
                delete n;
                n = src.n;
        }

        return *this;
}

int main()
{
        T t;
        T s;

        T m(std::move(t));
        
        T p = std::move(s);

        p.get();

        m.get();

        return 0;
}
